<template>
    <div class="ele-body">
        <el-card shadow="never">
            <Search @search="reload" />
            <ele-pro-table ref="table" :columns="columns" :datasource="datasource" :selection.sync="selection"
                :headerEllipsis="false" cache-key="appTable">
            </ele-pro-table>
        </el-card>
    </div>
</template>

<script>
import { pageGoldLog, } from '@/api/log';
import Search from './components/search.vue';
import { formatDate } from "@/utils/formate";
export default {
    name: 'GatherTable',
    components: { Search },
    data() {
        return {
            // 当前编辑数据
            current: null,
            // 是否显示编辑弹窗
            showEdit: false,
            // 表格列配置
            columns: [
                {
                    columnKey: 'selection',
                    type: 'selection',
                    width: 45,
                    align: 'center',
                    fixed: 'left'
                },
                {
                    columnKey: 'index',
                    type: 'index',
                    label: '序号',
                    width: 50,
                    align: 'center',
                    fixed: 'left',
                },
                {
                    prop: 'userId',
                    label: '用户ID',
                    align: 'center',
                    // width: 150
                },
                {
                    prop: 'before',
                    label: '之前金额',
                    align: 'center',
                    // width: 90,
                    showOverflowTooltip: true
                },
                {
                    prop: 'gold',
                    label: '花费金额',
                    align: 'center',
                    // width: 150,
                    showOverflowTooltip: true
                },
                {
                    prop: 'after',
                    label: '之后金额',
                    align: 'center',
                },

                {
                    prop: 'company',
                    label: '公司',
                    align: 'center',
                },
                {
                    prop: 'type',
                    label: '状态',
                    align: 'center',
                    formatter:(row)=>{
                        switch(row.type){
                            case 1:
                                return '游戏投入'
                            case 2:
                                return '游戏收入'
                            case 3:
                                return '购买会员'
                            case 4:
                                return '充值'
                            case 5:
                                return '提现'
                        }
                    }
                },
                {
                    prop: 'desc',
                    label: '备注',
                    align: 'center',
                },
                {
                    prop: 'createTime',
                    label: '创建时间',
                    // sortable: 'custom',
                    showOverflowTooltip: true,
                    minWidth: 110,
                    formatter: (row) => row.createTime ? formatDate(new Date(row.createTime)) : ''
                },
                // {
                //     columnKey: 'action',
                //     label: '操作',
                //     width: 120,
                //     align: 'center',
                //     resizable: false,
                //     slot: 'action',
                //     showOverflowTooltip: true
                // }
            ],
            // 表格选中数据
            selection: []
        };
    },
    methods: {
        /* 表格数据源 */
        datasource({ page, limit, where, order }) {
            return pageGoldLog({ ...where, ...order, page, limit })
        },
        /* 刷新表格 */
        reload(where) {
            this.$refs.table.reload({ page: 1, where: where });
        },
        /* 打开编辑弹窗 */
        openEdit(row) {
            this.current = row;
            this.showEdit = true;
        },
        /* 删除 */
        remove(row) {
            // const loading = this.$loading({ lock: true });
            removeGather({ ids: [row._id] })
                .then((msg) => {
                    // loading.close();
                    this.$message.success(msg);
                    this.reload();
                })
                .catch((e) => {
                    // loading.close();
                    this.$message.error(e.message);
                });
        },
        /* 批量删除 */
        removeBatch() {
            this.$confirm('确定要删除选中的应用吗?', '提示', {
                type: 'warning'
            })
                .then(() => {
                    const loading = this.$loading({ lock: true });
                    let ids = this.selection.map((d) => d._id)
                    removeGathers({ ids })
                        .then((msg) => {
                            loading.close();
                            this.$message.success(msg);
                            this.reload();
                        })
                        .catch((e) => {
                            loading.close();
                            this.$message.error(e.message);
                        });
                })
                .catch(() => { });
        },
        /**
         * 时间格式化
         * @param date
         */
        doFormatDate(date) {
            return date ? formatDate(new Date(date)) : "";
        },
        /**
         * 获取点击数量
         * @param daysData
         * @param time
         * @param startTime
         */
        onGetClick(daysData, time, startTime = 0) {
            let num = 0;
            if (daysData && JSON.stringify(daysData) !== "{}") {
                for (let i = startTime; i < time; i++) {
                    let day = utils.formatDate(new Date(new Date().getTime() - (1000 * 60 * 60 * 24 * i)), "yyyy-MM-dd")
                    if (daysData[day]) num += daysData[day].click || 0;
                }
            }
            return num;
        }
    }
};
</script>